<?php
/**
 *
 * @package    gadgets
 * @author     Daniel Botelho <botelho.daniel@gmail.com>
 */
class GadgetsIO
{
  const DEFAULT_CLASS = 'gadgets.io';
  
  //PT: Este método gera um GadgetRequest e adiciona-o automaticamente à view. Se for necessário que não adicione à view, mas que apenas gere o código, deve-se passar com o parámetro $inject=false
  /**
   * This static method generates the gadget request and append it to the view if $inject=true
   * @param GadgetRequest $gadgetRequest The gadget request
   * @param boolean $inject True, the gadget will be appended to the view 
   * @return GadgetResponse
   */
  public static function makeRequest(GadgetRequest $gadgetRequest,$inject = true)
  {
  	$request_js = $gadgetRequest->getJSRequestFunction();
  	$url = $gadgetRequest->getURLVarName();
  	$response = ($gadgetRequest->getJSResponseFunction()->isClosure())? $gadgetRequest->getJSResponseFunction()->__toString() : $gadgetRequest->getJSResponseFunction()->getName();
  	$params = $gadgetRequest->getParamsVarName();
  	$html = $gadgetRequest->generateRequest()."gadgets.io.makeRequest($url, $response, $params);\n";
  	
  	$request_js->addContent($html);
  	
  	if($inject){
  		echo JSFunction::addJSTags($request_js);
  	}
  	return new GadgetResponse($gadgetRequest);
  }
  
  public static function encodeValues($content,$appendable=false)
  {
  	$html = "gadgets.io.encodeValues($content)";
  	if(!$appendable)
  	  $html .= ";\n";
  	return $html;
  }
  
  public static function getProxyUrl($url,$appendable=false)
  {
  	$html = "gadgets.io.getProxyUrl($content)";
  	if(!$appendable)
  	  $html .= ";\n";
  	return $html;
  }
   
}